package com.jctrip.hotel.source.xxl;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.jctrip.hotel.common.enums.SourceEnum;
import com.jctrip.hotel.domain.entity.*;
import com.jctrip.hotel.source.service.basic.*;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jctrip.hotel.common.constant.XxlJobNameConstant;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.XxlJob;

import lombok.extern.slf4j.Slf4j;

import javax.xml.transform.Source;

/**
 * @Author henry
 * @Date 2023/6/29 20:11
 * @Description
 */
@Component
@Slf4j
public class SyncHtPriceCalendarDeleteXxlJob extends IJobHandler {
    @Autowired
    private HtPriceCalendarService htPriceCalendarService;

    @Autowired
    private HtPriceCalendarAtourService htPriceCalendarAtourService;

    @Autowired
    private HtPriceCalendarRezenService htPriceCalendarRezenService;

    @Autowired
    private HtPriceCalendarElongService htPriceCalendarElongService;

    @Autowired
    private HtPriceCalendarGreentreeService htPriceCalendarGreentreeService;

    @Autowired
    private HtPriceCalendarHmeinService htPriceCalendarHmeinService;

    @Autowired
    private HtPriceCalendarHworldService htPriceCalendarHworldService;

    @Override
    @XxlJob(XxlJobNameConstant.SYNC_HT_PRICE_CALENDAR_DELETE_TASK)
    public void execute() throws Exception {
        XxlJobHelper.log(XxlJobNameConstant.SYNC_HT_PRICE_CALENDAR_DELETE_TASK + " xxl job start");

        try {
            String nowDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
            // 主表历史房态
            htPriceCalendarService.remove(
                    new LambdaQueryWrapper<HtPriceCalendar>().lt(HtPriceCalendar::getCalDate, nowDate));
            // 亚朵房态
            htPriceCalendarAtourService.remove(
                    new LambdaQueryWrapper<HtPriceCalendarAtour>().lt(HtPriceCalendarAtour::getCalDate, nowDate));
            // 丽呈房态
            htPriceCalendarRezenService.remove(
                    new LambdaQueryWrapper<HtPriceCalendarRezen>().lt(HtPriceCalendarRezen::getCalDate, nowDate));
            // 艺龙房态
            htPriceCalendarElongService.remove(
                    new LambdaQueryWrapper<HtPriceCalendarElong>().lt(HtPriceCalendarElong::getCalDate, nowDate));
            // 如家房态
            htPriceCalendarHmeinService.remove(
                    new LambdaQueryWrapper<HtPriceCalendarHmein>().lt(HtPriceCalendarHmein::getCalDate, nowDate));
            // 华住房态
            htPriceCalendarHworldService.remove(
                    new LambdaQueryWrapper<HtPriceCalendarHworld>().lt(HtPriceCalendarHworld::getCalDate, nowDate));


            // 格林房态
            htPriceCalendarGreentreeService.remove(new LambdaQueryWrapper<HtPriceCalendarGreentree>()
                    .lt(HtPriceCalendarGreentree::getCalDate, nowDate));
            htPriceCalendarGreentreeService.update(null, new LambdaUpdateWrapper<HtPriceCalendarGreentree>()
                    .set(HtPriceCalendarGreentree::getQuota, 1)
                    .ne(HtPriceCalendarGreentree::getQuota, 0));
            htPriceCalendarAtourService.update(null, new LambdaUpdateWrapper<HtPriceCalendarAtour>()
                    .set(HtPriceCalendarAtour::getQuota, 1)
                    .ne(HtPriceCalendarAtour::getQuota, 0));
            htPriceCalendarService.update(null, new LambdaUpdateWrapper<HtPriceCalendar>()
                    .set(HtPriceCalendar::getQuota, 1)
                    .likeRight(HtPriceCalendar::getHotelId, SourceEnum.WEHOTEL.getCode())
                    .ne(HtPriceCalendar::getQuota, 0));
            htPriceCalendarElongService.update(null, new LambdaUpdateWrapper<HtPriceCalendarElong>()
                    .set(HtPriceCalendarElong::getQuota, 0));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        XxlJobHelper.log(XxlJobNameConstant.SYNC_HT_PRICE_CALENDAR_DELETE_TASK + " xxl job end");

    }
}
